// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
namespace Microsoft.Xrm.Sdk
{
    using System.Globalization;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk.Temporary;

	/// <summary>
	/// Represents a relationship to use when associating, disassociating and retrieving related entities
	/// </summary>
	[DataContract(Name = "Relationship", Namespace = XmlNamespaces.V5.Contracts)]
	public sealed class Relationship : IExtensibleDataObject
	{
		/// <summary>
		/// Creates a Relationship object
		/// </summary>
		public Relationship()
		{
		}

		/// <summary>
		/// Creates a Relationship object and initilizes SchemaName member
		/// </summary>
		/// <param name="schemaName">Schema name for the relationship.</param>
		public Relationship(string schemaName)
		{
			_schemaName = schemaName;
		}

		/// <summary>
		/// Schema name for the relationship
		/// </summary>
		[DataMember]
		public string SchemaName
		{
			get { return _schemaName; }
			set { _schemaName = value; }
		}

		/// <summary>
		/// Role of primary entity in the relationship.
		/// Required when using a reflexive relationship
		/// Not required for non-reflexive relationship, must match the entity's role if specified
		/// </summary>
		[DataMember]
		public EntityRole? PrimaryEntityRole
		{
			get { return _primaryEntityRole; }
			set { _primaryEntityRole = value; }
		}

		#region usability methods
		public override bool Equals(object obj)
		{
			Relationship other = obj as Relationship;
			if (other == null)
			{
				return false;
			}
			return ((this.SchemaName == other.SchemaName)
				&& (this._primaryEntityRole == other._primaryEntityRole));
		}

		public override int GetHashCode()
		{
			int hashCode = (_schemaName ?? string.Empty).GetHashCode();
			if (_primaryEntityRole.HasValue)
			{
				hashCode ^= _primaryEntityRole.Value.GetHashCode();
			}
			return hashCode;
		}

		public override string ToString()
		{
			return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", _schemaName ?? string.Empty, _primaryEntityRole.HasValue ? _primaryEntityRole.ToString() : string.Empty);
		}
		#endregion

		#region private members
		private EntityRole? _primaryEntityRole;
		private string _schemaName;
		#endregion

		#region IExtensibleDataObject Implementation
		private ExtensionDataObject ExtensionDataObject;
		public ExtensionDataObject ExtensionData
		{
			get
			{
				return ExtensionDataObject;
			}
			set
			{
				ExtensionDataObject = value;
			}
		}
		#endregion IExtensibleDataObject Implementation
	}
}
